
"""
Copyright 2009 Andrew T. Barker

This file is part of Second Budget.

Second Budget is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Second Budget is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Second Budget, in the file LICENSE.  If not, see 
<http://www.gnu.org/licenses/>.
"""

import commands,os,sys

class SBConfig:

    def __init__(self,configfile=None,interactive=False):        
        if configfile==None:
            # look for init file in a few places, 
            pass
        # self.loadfromfile(filename)
        if interactive:
            # self.configinteractive()
            pass
        self.autoconfig()

    def autoconfig(self):
        """
        Figures out reasonable defaults, from the environment, without loading file.
        """

        self.stampfmt = "%d%b%Yat%H.%M"
        self.logfile = "secondbudget.log"
        self.logging = True

        # for Windows consider boxcutter (http://rasm.ods.org/boxcutter/)
        #      or ImageGrab (http://www.pythonware.com/library/pil/handbook/imagegrab.htm)
        #      or screenshot-cmd (http://code.google.com/p/screenshot-cmd/)
        # for Mac OS X consider /usr/sbin/screencapture -x screenshot.png
        self.knownplatforms = ["linux"]
        self.ssdirs = ["./screenshots/"]
        self.ssutils = {"linux":["scrot","xwd"]}
        self.ssformats = {"scrot":"jpg",
                          "xwd":"xwd"}
        # note this dictionary assumes filename at end of invocation, which may
        # not generally be true, should perhaps revise
        self.ssinvocations = {"scrot":"scrot ",
                              "xwd":"xwd -root -silent -out "}
        # xwd requires some conversion before it can be used in sbscore.py---see try.scm, try.sh

        self.prefix = "ss"
        self.maxshots = 150
        self.maxtimerunning = 10
        self.mininterval = 4
        self.maxinterval = 35        

        # choose directory
        self.directory = None
        for dir in self.ssdirs:
            if os.path.exists(dir):
                self.directory = dir
                break
        if self.directory == None:
            self.directory = "./"
        if not self.directory[-1] == "/":
            self.directory = self.directory + "/"

        # figure out platform
        givenplat = sys.platform
        self.platform = None
        for plat in self.knownplatforms:
            if plat in givenplat:
                self.platform = plat
                break
        if self.platform == None:
            print "Unknown platform---send me an email to try to figure this out."        

        # based on platform, choose screenshot utility
        self.screenshottool = None
        for ssutil in self.ssutils[self.platform]:
            pair = commands.getstatusoutput("which " + ssutil)
            if pair[0] == 0:
                self.screenshottool = ssutil
                break
        if self.screenshottool == None:
            print "Can't find a screenshot utility for your platform."

        # based on screenshot utility, choose file format and command line
        self.format = self.ssformats[self.screenshottool]
        self.invoke = self.ssinvocations[self.screenshottool]
        
    def loadfromfile(self,filename):
        pass

    def savetofile(self,filename):
        pass

def main():
    print "Second Budget configuration module."
    # probably want to do interactive config in this case, then save file

if __name__ == "__main__":
    main()
